/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-01-19
 * Time: 9:14
 */
public class MyLinkedList {

    MyLinkedList.InnerList node1 = new MyLinkedList.InnerList();
    MyLinkedList.InnerList node2 = new MyLinkedList.InnerList();
    MyLinkedList.InnerList node3 = new MyLinkedList.InnerList();
    MyLinkedList.InnerList node4 = new MyLinkedList.InnerList();
    MyLinkedList.InnerList node5 = new MyLinkedList.InnerList();
    private InnerList head;
    MyLinkedList.InnerList cur = new MyLinkedList.InnerList();


    public static class InnerList {
        private int val;
        private InnerList next;

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public InnerList getNext() {
            return next;
        }

        public void setNext(InnerList next) {
            this.next = next;
        }
    }
    public MyLinkedList() {

        node1.setVal(12);
        node2.setVal(23);
        node3.setVal(34);
        node4.setVal(45);
        node5.setVal(56);

        node1.setNext(node2);
        node2.setNext(node3);
        node3.setNext(node4);
        node4.setNext(node5);
        head = node1;
    }
        //头插法
        public void addFirst(int data) {
        MyLinkedList.InnerList node = new MyLinkedList.InnerList();
        node.setVal(data);
        node.setNext(head);
        head = node;
        cur = head;
        }
        //尾插法
        public void addLast(int data) {
        MyLinkedList.InnerList node = new MyLinkedList.InnerList();
        node.setVal(data);
        while (cur.next != null) {
            cur =cur.next;
        }
        cur.next = node;
        cur = head;
        }
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data) {
        InnerList node = new MyLinkedList.InnerList();
        node.setVal(data);
        if(index == 0) {
            addFirst(data);
            return ;
        }
        while (index - 1 > 0) {
            cur = cur.next;
            index--;
        }
            node.next = cur.next;
            cur.next = node;
            cur = head;
        }
//        查找是否包含关键字key是否在单链表当中
        public boolean contains(int key) {
            while (cur != null) {
                if(cur.val == key) {
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }
        //删除第一次出现关键字为key的节点
        public void remove(int key) {
            while (cur.next != null) {
                if(cur.next.val == key) {
                    cur.next = cur.next.next;
                    break;
                }
                cur = cur.next;
            }
            cur = head;
        }
        //删除所有值为key的节点
        public void removeAllKey(int key) {
            while (cur.next != null) {
                if(cur.next.val == key) {
                    cur.next = cur.next.next;
                }
                cur = cur.next;
            }
            cur = head;
        }
//        //得到单链表的长度
        public int size() {

        int count = 0;
            while (cur.next != null) {
                count++;
                cur = cur.next;
            }
            return  count;
        }
        public void display() {
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
        cur = head;
        }
        public void clear() {
            while (cur!= null) {
                 MyLinkedList.InnerList t = cur;
                cur = cur.next;
                t = null;
            }
            head = null;
        }
}
